﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<DomainModel.Order>>" %>
<asp:Content  ContentPlaceHolderID="TitleContent" runat="server">
	Список заказов
</asp:Content>
<asp:Content ContentPlaceHolderID = "HeadContent" runat="server">
<link rel="stylesheet" type="text/css" media="screen" href="../../Content/jquery-ui-1.8.4.custom.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="../../Content/ui.jqgrid.css"/>
	<style type="text/css">html, body {
	    margin: 0;
	    padding: 0;
	}
	input {
	padding:0.15em;
	border:1px solid #ddd;
	background:#fafafa;
	font:bold 0.95em arial, sans-serif;
	-moz-border-radius:0.4em;
	-khtml-border-radius:0.4em;
    }
    input:hover, input:focus {
	border-color:#c5c5c5;
	background:#f6f6f6;
    }     
    select 
    {
    	border:1px solid #c5c5c5; 
    	background-color:#f6f6f6;
    	-moz-border-radius:0.4em;
	    -khtml-border-radius:0.4em; 
	    font:bold 0.95em arial, sans-serif;
	    font-size:small;	  
	    padding:0.15em;	 
     }
     select option 
     {
     	border-bottom:1px solid #dadada;
     	height:1.3em;
     }
	</style>
	<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
	<script type="text/javascript" src="../../Scripts/i18n/grid.locale-ru.js"></script>
	<script type="text/javascript" src="../../Scripts/jquery.jqGrid.min.js"></script>
    <script src="../../Scripts/jquery-ui-1.8.4.custom.min.js" type="text/javascript"></script>
    <script src="../../Scripts/i18n/jquery.ui.datepicker-ru.js" type="text/javascript"></script>
    <script type="text/javascript">
       
var additem=false;var lastselect=new Object();$(function(){$('#btn_exit').button({icons:{primary:'ui-icon-circle-close'}});$('#btnacceptfilter').button({icons:{primary:'ui-icon-circle-check'}});$('.btnadd').button({icons:{primary:'ui-icon-circle-plus'}});createmanagers();createusers();var bla='<%=this.ViewData["StatusList"] %>';var editoptStatuses={value:bla};var manager=Boolean(<%=this.ViewData["Manager"]%>);$("#datefrom").datepicker({dateFormat:"dd/mm/yy",showOn:'both',buttonImage:'../../Content/images/Calendar.gif',buttonImageOnly:true});$("#dateto").datepicker({dateFormat:"dd/mm/yy",showOn:'both',buttonImage:'../../Content/images/Calendar.gif',buttonImageOnly:true});$('#datefrom').val("");$('#dateto').val("");var film=$('#filmanagers');if(film.length!=0)
film.val("");$('#filmanagers').val("");var filcl=$('#filclients');if(filcl.length!=0)
filcl.val("");$('#grid').jqGrid({url:'<%=Url.Action("OrderList_json", "Orders") %>',datatype:'json',height:400,autowidth:true,mtype:'POST',colNames:['','Номер заказа','Дата заказа','ФИО клиента','','Сумма','Статус заказа','Дата поставки','Признак оплаты'],colModel:[{name:'act',index:'act',width:85,sortable:false,search:false,sortable:false},{name:'OrderNum',index:'OrderNum',width:55,editable:false,sortable:false},{name:'Дата заказа',index:'Дата заказа',width:65,editable:false,sortable:false,editrules:{required:true}},{name:'ФИО клиента',index:'ФИО клиента',editable:false,sortable:false},{name:'Ratio',index:'Ratio',editable:false,sortable:false,hidden:true},{name:'Sum',index:'Sum',editable:false,sortable:false,width:60},{name:'OrderStatus',index:'OrderStatus',editable:true,sortable:false,edittype:"select",editoptions:editoptStatuses},{name:'datepos',index:'datepos',width:55,editable:manager,sortable:false,editrules:{required:false,date:true},sorttype:"date",datefmt:'dd/mm/yyyy'},{name:'Признак оплаты',index:'Признак оплаты',width:55,editable:manager,sortable:false,edittype:"checkbox",editoptions:{value:"Оплачено:Неоплачено"}}],pager:$('#gridPager'),multiselect:false,afterInsertRow:function(rowid)
{initRowEditMode('#grid',rowid,manager);},subGrid:true,subGridRowExpanded:function(subgrid_id,row_id){var subgrid_table_id;var lastsels;subgrid_table_id=subgrid_id+"_t";jQuery("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table>");jQuery("#"+subgrid_table_id).jqGrid({url:'<%=Url.Action("OrderDetailsList_json", "Orders") %>'+'/'+row_id,datatype:"json",colNames:['','P/N','Наименование','Кол-во',<%if(this.ViewData["Manager"]!=""){%>'Цена поставщика',<%}%>'Цена','Сумма'<%if((bool)(this.ViewData["FinEnable"])){%>,'Прибыльность','Кредитность'<%}%>],postData:{'add':additem},autowidth:true,mtype:'POST',editurl:'<%=Url.Action("DetailListSave", "Orders") %>',colModel:[{name:'act',index:'act',width:60,sortable:false,search:false,editable:false},{name:'P/N',index:'P/N',width:150,editable:!manager,sortable:false,editrules:{required:true}},{name:'Наименование',index:'Наименование',width:200,editable:!manager,sortable:false,editrules:{required:true}},{name:'dcount',index:'dcount',width:50,editable:!manager,sortable:false,editrules:{required:true}},<%if(this.ViewData["Manager"]!=""){%>{name:'sPrice',index:'sPrice',width:120,editable:manager,sortable:false,editrules:{required:true}},<%}%>{name:'ePrice',index:'ePrice',width:60,editable:manager,sortable:false,editrules:{required:true}},{name:'dsum',index:'dsum',width:70,editable:false,sortable:false},<%if((bool)(this.ViewData["FinEnable"])){%>{name:'marzha',index:'marzha',width:90,editable:false,sortable:false},{name:'credit',index:'credit',width:90,editable:false,sortable:false}<%}%>],height:'100%',rowNum:100,ondblClickRow:function(id){if(lastselect['#'+subgrid_table_id]!=id){$('#ces'+lastselect['#'+subgrid_table_id]).hide();$('#'+subgrid_table_id).jqGrid('restoreRow',lastselect['#'+subgrid_table_id]);var data=$('#grid').getRowData(row_id);if(((!manager)&&(data.OrderStatus=="Создан"))||(manager))
{$('#ces'+id).show();$('#'+subgrid_table_id).jqGrid('editRow',id,true,false,false,false,null,function(){$('#ces'+id).hide();lastselect['#'+subgrid_table_id]=0;setadditionalcells(id,row_id,subgrid_table_id)},function(){$('#ces'+id).hide();lastselect['#'+subgrid_table_id]=0;},function(){$('#ces'+id).hide();lastselect['#'+subgrid_table_id]=0;});}}
lastselect['#'+subgrid_table_id]=id;},gridComplete:function(){var idss=jQuery('#'+subgrid_table_id).jqGrid('getDataIDs');var rowdata=$("#grid").getRowData(row_id);var del=false;var des=" ",ces=" ";switch(rowdata.OrderStatus){case'Не обработан':break;case'Создан':if(!manager)
{del=true;}
break;}
for(var i=0;i<idss.length;i++){var cl=idss[i];if(del)
des="<button class='desbtn' onclick=\"jQuery('#"+subgrid_table_id+"').jqGrid('delGridRow','"+cl+"',{ caption: 'Удаление позиции', msg:'Удалить выбранную позицию заказа?', bSubmit: 'Удалить',bCancel: 'Отмена', processData: 'Удалиние...'});\">Удалить</button>";ces="<button class='cesbtn' id='ces"+cl+"' onclick=\"cancelEdit_Click('#ces','"+cl+"','#"+subgrid_table_id+"');\">Отменить редактирование</button>";jQuery('#'+subgrid_table_id).jqGrid('setRowData',idss[i],{act:des+ces});$("#ces"+cl).hide();}
$(".cesbtn").button({icons:{primary:'ui-icon-cancel'},text:false});$(".desbtn").button({icons:{primary:'ui-icon-circle-minus'},text:false});}});},rowNum:30,rowList:[30,50,100],sortname:'ФИО пользователя',sortorder:'asc',enableSearch:false,caption:'Заказы',editurl:'<%=Url.Action("OrderListSave", "Orders") %>',ondblClickRow:function(id){if(lastselect['#grid']!=id){$('#grid').jqGrid('restoreRow',lastselect['#grid']);var data=$('#grid').getRowData(id);$('#ce'+lastselect['#grid']).hide();if(((!manager)&&(data.OrderStatus=="Создан"))||(manager))
{$('#ce'+id).show();$('#grid').editRow(id,true,pickdates,false,false,null,function(){$('#ce'+id).hide();lastselect['#grid']=0;;initRowEditMode('#grid',id,manager);$('#grid').collapseSubGridRow(id);},function(){$('#ce'+id).hide();lastselect['#grid']=0},function(){$('#ce'+id).hide();lastselect['#grid']=0;});}
lastselect['#grid']=id;}}}).navGrid("#gridPager",{edit:false,add:false,del:false,search:false});var wheight=$(window).height()-$(".div_header").height()-$("#filtermenu").height()-$(".btnadd").height()-120;$('#grid').setGridHeight(wheight);});function addsub(id){additem=true;if($('#grid_'+id+'_t').length==0){$('#grid').expandSubGridRow(id);}else{$('#grid_'+id+'_t').setPostDataItem('add',true);$('#grid_'+id+'_t').trigger("reloadGrid");}
additem=false;}
function cancelEdit_Click(id,cl,gridid){jQuery(gridid).jqGrid('restoreRow',cl);jQuery(id+cl).hide();lastselect[gridid]=0;}
function pickdates(id)
{jQuery("#"+id+"_datepos","#grid").datepicker({dateFormat:"dd/mm/yy"});}
function createmanagers()
{var manlist='<%=this.ViewData["ManagerList"] %>';var data=manlist.split(';');var output=[];var length=data.length;output[0]='<option value=\"0\">Любой</option>';for(var i=1;i<=length;i++)
{if(data[i-1]!="")
{var valt=data[i-1].split(':');output[i]='<option value="'+valt[0]+'">'+valt[1]+'</option>';}}
$('#filmanagers').append(output.join(''));}
function createusers()
{var manlist='<%=this.ViewData["UserList"] %>';var data=manlist.split(';');var output=[];var length=data.length;output[0]='<option value=\"0\">Любой</option>';for(var i=1;i<=length;i++)
{if(data[i-1]!="")
{var valt=data[i-1].split(':');output[i]='<option value="'+valt[0]+'">'+valt[1]+'</option>';}}
$('#filclients').append(output.join(''));}
function acceptFilter_click()
{var filcl=$('#filclients');if(filcl.length==0)
filcl.val(0);var film=$('#filmanagers');if(film.length==0)film.val(0);$('#grid').setPostDataItem('datefrom',$('#datefrom').val());$('#grid').setPostDataItem('dateto',$('#dateto').val());$('#grid').setPostDataItem('filmanagers',film.val());$('#grid').setPostDataItem('filclients',filcl.val());$('#grid').trigger("reloadGrid");}
function initRowEditMode(gridid,rowid,manager)
{var as="",de=" ",ce=" ";var rowdata=$(gridid).getRowData(rowid);switch(rowdata.OrderStatus){case'Не обработан':break;case'Создан':if(!manager)
{as="<button id='se"+rowid+"' onclick=\"addsub('"+rowid+"')\">Добавить позицию</button>";de="<button id='de"+rowid+"' onclick=\"jQuery('"+gridid+"').jqGrid('delGridRow','"+rowid+"',{ caption: 'Удаление заказа', msg:'Удалить выбранный заказ?<br\>Все позиции будут также удалены.', bSubmit: 'Удалить',bCancel: 'Отмена', processData: 'Удалиние...'});\">Удалить</button>";}
break;}
ce="<button id='ce"+rowid+"' onclick=\"cancelEdit_Click('#ce','"+rowid+"','"+gridid+"');\">Отменить редактирование</button>";jQuery(gridid).jqGrid('setRowData',rowid,{act:as+de+ce});$("#ce"+rowid).button({icons:{primary:'ui-icon-cancel'},text:false});$("#se"+rowid).button({icons:{primary:'ui-icon-circle-plus'},text:false});$("#de"+rowid).button({icons:{primary:'ui-icon-circle-minus'},text:false});$("#ce"+rowid).hide();}
function roundTo2(float)
{return Math.round(float*100)/100}
function setadditionalcells(id,row_id,subgrid_table_id)
{<%if((bool)(this.ViewData["FinEnable"])){%>var fin=true<%}else{%>var fin=false;<%}%>var rate=$('#grid').getRowData(row_id).Ratio;var srdata=$('#'+subgrid_table_id).getRowData(id);var sum=srdata.dcount*parseFloat(srdata.ePrice.replace(',','.'));$('#'+subgrid_table_id).setRowData(id,{dsum:roundTo2(sum)});if(fin)
{var smarzha=sum-srdata.dcount*parseFloat(srdata.sPrice.replace(',','.'));$('#'+subgrid_table_id).setRowData(id,{marzha:roundTo2(smarzha),credit:roundTo2(smarzha*parseFloat(rate.replace(',','.')))});}
var coldata=jQuery('#'+subgrid_table_id).getCol('dsum',false,'');var summ=0.00;for(var i=0;i<coldata.length;i++)
{summ+=parseFloat(coldata[i].replace(',','.'));}
$('#grid').setRowData(row_id,{Sum:roundTo2(summ)});}

        
    </script>
	
</asp:Content>
<asp:Content  ContentPlaceHolderID="MainContent" runat="server">
  <table id="filtermenu">
<tr>
<% if (this.ViewData["ManagerList"]!=null) { %>    
<td><label class="ui-widget" style="font-size:small">Менеджеры:</label></td>
<td>
         <select id="filmanagers"/>
     
</td>
<% } %>   
<% if (this.ViewData["UserList"]!=null) { %>
<td><label class="ui-widget" style="font-size:small">Клиенты:</label></td>
<td>
       <select id="filclients"/>
</td>
 <% } %>   
<td><label class="ui-widget" style="font-size:small">Заказы с</label></td>
<td>
   <input type="text" id="datefrom"/>
</td>
<td><label class="ui-widget" style="font-size:small">Заказы по</label></td>
<td>
    <input type="text" id="dateto"/>
</td>
<td>
  
 <button id="btnacceptfilter" onclick="acceptFilter_click()" style="font-size:small">Применить</button>        
         
</td>
</tr>
</table>
<% if (string.IsNullOrEmpty(this.ViewData["Manager"].ToString())) { %> <a href='<%= Url.Action("Add", "Orders") %>' class='btnadd'>Добавить заказ</a> <% } %>   
<table id="grid"></table>
<div id="gridPager"></div>
</asp:Content>
